package com.newx.auth.license.tool;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * TD9RbFQZcDl+TnHoYPL2ArRVZBjpP+oHGKVU5kf9iXU=
 * @Created by yxl
 * @Date 2025/2/15 17:13
 */
public class LicenseEncryptorTool {

    public static final String ALGORITHM = "AES";
    public static final int KEY_SIZE = 256;

    public static SecretKey generateKey() throws NoSuchAlgorithmException {
        KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM);
        keyGen.init(KEY_SIZE);
        return keyGen.generateKey();
    }

//    public static void main(String[] args) {
//        try {
//            SecretKey secretKey = generateKey();
//            System.out.println("Generated AES Key: " + Base64.getEncoder().encodeToString(secretKey.getEncoded()));
//        } catch (NoSuchAlgorithmException e) {
//            e.printStackTrace();
//        }
//    }
    public static String encrypt(String data, SecretKey secretKey) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedData = cipher.doFinal(data.getBytes());
        return Base64.getEncoder().encodeToString(encryptedData);
    }

    public static String decrypt(String encryptedData, SecretKey secretKey) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decodedData = Base64.getDecoder().decode(encryptedData);
        byte[] decryptedData = cipher.doFinal(decodedData);
        return new String(decryptedData);
    }

    public static void main(String[] args) {
        try {
            SecretKey secretKey1 = generateKey();
            System.out.println("Generated AES Key: " + Base64.getEncoder().encodeToString(secretKey1.getEncoded()));
            // 假设你已经有了一个SecretKey对象
            SecretKey secretKey = new SecretKeySpec(Base64.getDecoder().decode("RP/SdfNBYnX7dp8E21jkIDXMdB3TVPv4OuC1PL9VxVo="), ALGORITHM);

            // 许可证内容
            String licenseContent = "{\"licenseId\":\"123456\",\"hardwareFingerprint\":\"2e018f7d00964feb2028a69d05dc495554f19316ab8fca66c3b6bd9807bca844\",\"validFrom\":1735660800000,\"validTo\":1767196800000,\"signature\":\"VaSlLgBSV0r7CLWbk+koVmH92qKOmhbX4X+1m8rHbFabMLv/VEektkB1yBsJgLf/08XYPVuTNYBZ5r0IouKc4+bY8mV0B6QUO8PwUHp9i/7ABIuFIP3QQUVL0d74u1qdPCn7br8wz5Z74316qo/43vmLaWuPmw27ZVTJnxoEY4PtU/RCSPAEt/cUkO3ooPr0fxiKUgr0zoy2vVr3Vv+PkSoHHwWTMG13pB5K2Wc4AxO9kBeBsgZ+T66/GlKNTuDBl42feCvh5Zrtfoo9UniFdsy06/Z8bHRseLnkTHIkdWjzHeb+WBc6YFCIqL0CFl/hIqyp7tWNKFcviOKMY+kkrA==\"}";

            // 加密
            String encryptedLicense = encrypt(licenseContent, secretKey);
            System.out.println("Encrypted License: " + encryptedLicense);

            // 解密
            String decryptedLicense = decrypt(encryptedLicense, secretKey);
            System.out.println("Decrypted License: " + decryptedLicense);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
